/** 用来收集依赖,收集的是一个个watcher*/
let id = 0;
class Dep/*←dependent?*/ {
    constructor() {
        this.id = id++;
        this.subs = [];
    }

    addSub(watcher) {
        this.subs.push(watcher);
    }

    notify(){
        this.subs.forEach(watcher => watcher.update());
    }

    depend(){
        if(Dep.target) { // 为了防止直接调用depend方法 先判断一下
            // Dep.target是一个渲染watcher
            Dep.target.addDep(this); // 希望可以在watcher中互相记忆
        }
    }
}

// 用来保存当前的watcher
// 防止在依赖收集时被第三者插足?
const stack = [];
export function pushTarget(watcher) {
    Dep.target = watcher;
    stack.push(watcher);
}

export function popTarget(){
    stack.pop();
    Dep.target = stack[stack.length - 1];
}

export default Dep;